﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebExam.BusinessObjects;
using WebExam.BusinessObjects.Constants;
using WebExam.Core.Extensions;
using WebExam.Core.Security;
using WebExam.DataEntities;
using WebExam.DataProvider;

namespace WebExam.BusinessServices
{
    public class ZGService
    {
        private readonly IZGRepository _zgRepository;

        public ZGService(IZGRepository zgRepository)
        {
            _zgRepository = zgRepository;
        }

        public PageResult<ZG> GetAll(int page)
        {
            var total = _zgRepository.Count();

            var start = PagerHelper.GetStartIndex(page, total, Constant.PAGE_SIZE);

            var result = new PageResult<ZG> { PageSize = Constant.PAGE_SIZE, TotalCount = total };

            result.List = _zgRepository.GetAll(start, result.PageSize).ToList();

            return result;
        }

        public bool IsLogin(string account, string pwd)
        {
            if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(pwd))
            {
                return false;
            }

            pwd = CryptographyManager.EncodePassowrd(pwd);

            var zg = _zgRepository.Get(account);

            return zg != null && string.Equals(pwd, zg.ZGPwd, StringComparison.Ordinal);
        }

        public bool IsExists(string account)
        {
            return _zgRepository.IsExists(account);
        }

    }
}